package com.atguigu.juc.volatiledemo;

/**
 * @Description:
 * @author： yeswater
 * @create： 2024/5/30
 */

public class SafeDoubleCheckSingleton {
	
	private static volatile SafeDoubleCheckSingleton instance;
	
	private SafeDoubleCheckSingleton() {}
	
	public static SafeDoubleCheckSingleton getInstance() {
		if (instance == null) {
			synchronized (SafeDoubleCheckSingleton.class) {
				if (instance == null) {
					// 隐患：多线程 由于重排序 该对象可能还未初始化就被其他线程读取
					// volatile 修饰 禁止生成对象时指令重排序
					instance = new SafeDoubleCheckSingleton();
				}
			}
		}
		return instance;
	}
	
}
